/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admintool.imp03_data;

import java.awt.Component;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import admintool.AdminToolView;

/**
 *
 * @author Administrator
 */
public class IMP03_Platform extends Component {

    private static final long serialVersionUID = 4584847317116209536L;
    private static ArrayList<IMP03_Platform> availablePlatforms = new ArrayList<IMP03_Platform>();

    /**
     * 
     * @param reload
     * @return
     */
    public static ArrayList<IMP03_Platform> getAllAvailablePlatformsFromDB(boolean reload){
        PreparedStatement ps = null;
        ResultSet rs = null;
        
        try {
            if(reload == true || availablePlatforms.isEmpty()){
                availablePlatforms.clear();
                ps = AdminToolView.getDBConnection().prepareStatement("SELECT * FROM platform");
                rs = ps.executeQuery();
                while(rs.next()){
                    availablePlatforms.add(new IMP03_Platform(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4)));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(rs != null){
                    rs.close();
                }
                if(ps != null){
                    ps.close();
                }
            } catch (Exception e) {
            }
        }
        
        return availablePlatforms;
    }
    
        /**
         * 
         * @return
         */
        public static IMP03_Platform getEmptyPlatformObject(){
        return new IMP03_Platform(-1, AdminToolView.EMPTY, AdminToolView.EMPTY, AdminToolView.ZERO);
    }

    private int platformID = -1;
    private String name = "";
    private String desc = "";
    private int cost = 0; // in EUR

    private ArrayList<IMP03_Vacancy> vacancyPlatforms = new ArrayList<IMP03_Vacancy>();
    private ArrayList<IMP03_Candidate> candidatePlatforms = new ArrayList<IMP03_Candidate>();
    
    private IMP03_Platform(int platformID, String platformName, String platformDesc, int cost) {
        this.platformID = platformID;
        this.name = platformName;
        this.desc = platformDesc;
        this.cost = cost;
    }

    /**
     * 
     * @return
     */
    public String getPlatformDesc() {
        return desc;
    }

    /**
     * 
     * @return
     */
    public int getPlatformID() {
        return platformID;
    }

    /**
     * 
     * @return
     */
    public String getPlatformName() {
        return name;
    }
    
    /**
     * 
     * @return
     */
    public int getPlatformCost() {
    	return this.cost;
    }

    @Override
    public String toString() {
        return getPlatformName() + " (costs: " + getPlatformCost() + " €)";
    }

    /**
     * 
     * @return
     */
    public ArrayList<IMP03_Vacancy> getVacancyPlatforms(){
        initPlaformVacancies();
        return vacancyPlatforms;    
    }
    
    /**
     * 
     * @return
     */
    public ArrayList<IMP03_Candidate> getCandidatePlatforms(){
        initPlaformCandidates();
        return candidatePlatforms;    
    }
    
    private void initPlaformVacancies() {
        PreparedStatement ps = null;
        ResultSet rs = null;
        vacancyPlatforms.clear();
        
        try {
            ps = AdminToolView.getDBConnection().prepareStatement("SELECT VacancyID FROM vacancy_platforms WHERE PlatformID = ?");
            ps.setInt(1, getPlatformID());
            rs = ps.executeQuery();
            ArrayList<Integer> vacancyIDs = new ArrayList<Integer>();
            while(rs.next()){
                vacancyIDs.add(rs.getInt(1));
            }
            
            for(IMP03_Vacancy v : IMP03_Vacancy.getAllAvailableVacanciesFromDB(false)){
                if(vacancyIDs.contains(v.getVacancyID())){
                    vacancyPlatforms.add(v);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                if(rs != null){
                    rs.close();
                }
                if(ps != null){
                    ps.close();
                }
            } catch (Exception e) {
            }
        }
    }
    
    private void initPlaformCandidates() {
        PreparedStatement ps = null;
        ResultSet rs = null;
        candidatePlatforms.clear();
        
        try {
            ps = AdminToolView.getDBConnection().prepareStatement("SELECT CandidateID FROM platform_candidates WHERE PlatformID = ?");
            ps.setInt(1, getPlatformID());
            rs = ps.executeQuery();
            ArrayList<Integer> candidatesIDs = new ArrayList<Integer>();
            while(rs.next()){
                candidatesIDs.add(rs.getInt(1));
            }
            
            for(IMP03_Candidate c : IMP03_Candidate.getAllAvailableCandidatesFromDB(false)){
                if(candidatesIDs.contains(c.getCandidateID())){
                    candidatePlatforms.add(c);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                if(rs != null){
                    rs.close();
                }
                if(ps != null){
                    ps.close();
                }
            } catch (Exception e) {
            }
        }
    }    
}